import pandas as pd
import plotly.express as px
from plotly.offline import init_notebook_mode
init_notebook_mode()
# Read the CSV file with proper parameters
df = pd.read_csv('../data/capture_fisheries.csv', skiprows=4) # Skip the header rows
df = df.dropna(how='all') # Drop empty rows
# Melt the DataFrame to transform years into rows
years = [str(year) for year in range(1960, 2025)]
df_melted = df.melt(
id_vars=["Country Name", "Country Code", "Indicator Name", "Indicator Code"],
value_vars=years,
var_name="Year",
value_name="Capture Production (metric tons)"
)
# Convert to numeric and clean data
df_melted['Year'] = pd.to_numeric(df_melted['Year'])
df_melted['Capture Production (metric tons)'] = pd.to_numeric(
df_melted['Capture Production (metric tons)'], errors='coerce'
)
df_melted = df_melted.dropna(subset=['Capture Production (metric tons)'])
# Calculate world totals by year
world_totals = df_melted.groupby('Year')['Capture Production (metric tons)'].sum().reset_index()
# Create the visualization
fig = px.line(
world_totals,
x='Year',
y='Capture Production (metric tons)',
title='World Total Capture Fisheries Production (1960-2024)',
labels={'Capture Production (metric tons)': 'Total Capture (metric tons)'},
markers=True
)
# Customize the layout
fig.update_layout(
xaxis_title='Year',
yaxis_title='Total Capture Production (metric tons)',
hovermode='x unified',
showlegend=False,
template='plotly_white'
)
fig.update_traces(
hovertemplate='<b>Year:</b> %{x}<br><b>Total Capture:</b> %{y:,} metric tons'
)
fig.show()